package com.kuanter.kuanterauto.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.NumberKeyListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.kuanter.kuanterauto.R;
import com.kuanter.kuanterauto.constants.MainConstants;
import com.umeng.analytics.MobclickAgent;
/**
 * 选择车牌号
 * @author kuanter
 *
 */
public class LincensePlateNumberActivity extends Activity implements OnClickListener{
	private static final int CHOOSE_PROVINCE = 0;
	private Button bt_sheng;
	private Button back_btn;
	private Button finish_btn;
	private String province = "鄂"; //省会简称
	private String car_number = ""; //车牌号（省会字母代号+车牌数字号）
	private int position;
	private EditText city_sym;
	private EditText et_cpnum;
	private RelativeLayout rl_xgcp;
	private Intent intent;
	private String sym = ""; //省会字母代号
	private String cpnum = "";//车牌数字号

	public void onResume() {
	    super.onResume();
	    MobclickAgent.onResume(this);
	}
	public void onPause() {
	    super.onPause();
	    MobclickAgent.onPause(this);
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.choose_license_plate_number);
		//getintent
		intent = getIntent();
		bt_sheng = (Button)this.findViewById(R.id.choose_province);
		bt_sheng.setOnClickListener(this);
		back_btn = (Button)this.findViewById(R.id.back_btn);
		back_btn.setOnClickListener(this);
		finish_btn = (Button)this.findViewById(R.id.finish_btn);
		finish_btn.setTextColor(getResources().getColor(R.color.white));
		finish_btn.setOnClickListener(this);
		position = getIntent().getIntExtra("position", -1);
		rl_xgcp = (RelativeLayout)this.findViewById(R.id.rl_xgcp);
		rl_xgcp.setBackgroundResource(R.drawable.topyuanjiao);
		//车牌号
		city_sym = (EditText)this.findViewById(R.id.city_sym);
		et_cpnum = (EditText)this.findViewById(R.id.et_cpnum);
		if(null != intent.getStringExtra("LicensePlateNumber")&&!"".equals(intent.getStringExtra("LicensePlateNumber"))){
			String LicensePlateNumber = intent.getStringExtra("LicensePlateNumber");
			province = LicensePlateNumber.substring(0, 1);
			sym = LicensePlateNumber.substring(1,2);
			cpnum = LicensePlateNumber.substring(2);
			bt_sheng.setText(province);
			city_sym.setText(sym);
			et_cpnum.setText(cpnum);
		}
		city_sym.setOnFocusChangeListener(new OnFocusChangeListener() {
			
			@Override
			public void onFocusChange(View arg0, boolean arg1) {
				if(!arg1){
					city_sym.setText(city_sym.getText().toString().toUpperCase());
				}
			}
		});
		
		et_cpnum.setKeyListener(new NumberKeyListener() {
			// 0无键盘 1英文键盘 2模拟键盘 3数字键盘
			@Override
			public int getInputType() {
				return 1;
			}
			// 返回允许输入的字符
			@Override
			protected char[] getAcceptedChars() {
				// TODO Auto-generated method stub
				char[] c = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','L','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','l','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9'};
				return c;
			}
		});
		et_cpnum.setOnFocusChangeListener(new OnFocusChangeListener() {
			
			@Override
			public void onFocusChange(View arg0, boolean arg1) {
				if(!arg1){
					et_cpnum.setText(et_cpnum.getText().toString().toUpperCase());
				}
			}
		});
		
		et_cpnum.setSelection(et_cpnum.getText().toString().length());
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back_btn:
			this.finish();
			break;
		case R.id.finish_btn:
//			Toast.makeText(this, "选择车牌号完成", 0).show();
			car_number = city_sym.getText().toString();
			car_number = car_number + et_cpnum.getText().toString();
			if("".equals(bt_sheng.getText().toString())){
				Toast.makeText(this, "请选择车牌省份", 0).show();
			}else if("".equals(city_sym.getText().toString())){
				Toast.makeText(this, "请输入车牌字母", 0).show();
			}else if(et_cpnum.getText().toString().length()!= 5){
				Toast.makeText(this, "请输入正确的车牌号", 0).show();
			}else {
				Intent intent = new Intent();
				intent.putExtra("province", province);
				intent.putExtra("car_number", car_number.toUpperCase());
				intent.putExtra("position", position);
				setResult(MainConstants.SUCCESS, intent);
				
				this.finish();
			}
			
			break;
		case R.id.choose_province:
			Intent intent = new Intent(this,XzsfActivity.class);
			startActivityForResult(intent, CHOOSE_PROVINCE);
			break;
		default:
			break;
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if(null != data){
			if(requestCode == CHOOSE_PROVINCE){
				province = data.getStringExtra("province");
				bt_sheng.setText(province);
			}
		}
		
		super.onActivityResult(requestCode, resultCode, data);
	}
}	
